//
//  ASJUSkillRain.m
//  ASJ001
//
//  Created by zephyrhan on 11/28/11.
//  Copyright (c) 2011 ebrothers. All rights reserved.
//

#import "ASJUSkillRain.h"
#import "ASJMonster.h"
#import "ASJGlobalDefine.h"


@implementation ASJUSkillRain


- (CCParticleSystem*)rainParticle {
    CCParticleSystem* rain = [[CCParticleSystemQuad alloc] initWithTotalParticles:600];
    
    rain.texture = [[CCTextureCache sharedTextureCache] addImage:@"rain_t.png"];;
    rain.emissionRate = 200;
    rain.angle = 280;
    rain.angleVar = 5.0;
    rain.blendFunc = (ccBlendFunc){GL_ONE, GL_ONE_MINUS_SRC_ALPHA};
    rain.duration = kCCParticleDurationInfinity;
    rain.emitterMode = kCCParticleModeGravity;
    rain.startColor = (ccColor4F){0.77, 0.86, 1.0, 0.7};
    rain.startColorVar = (ccColor4F){0.0, 0.0, 0.0, 0.0};
    rain.endColor = (ccColor4F){1.0, 1.0, 1.0, 0.2};
    rain.startSize = 2.0;
    rain.startSizeVar = 1.0;
    rain.endSize = 0.5;
    rain.endSizeVar = 0.5;
    rain.gravity = ccp(10.0, -10.0);
    rain.radialAccel = 10.0;
    rain.radialAccelVar = 1.0;
    rain.speed = 800;
    rain.speedVar = 100;
    rain.tangentialAccel = 10.0;
    rain.tangentialAccelVar = 1.0;
    rain.life = 4.5;
    
    rain.position = worldPos(0.5, 1.0);
    rain.posVar = ccp(winX(0.5), 0.0);
    
    return [rain autorelease];
}


- (void)skillEffect {
    if((self = [super init])) {
        //添加下雨粒子效果
        //长条雨滴，速度较快斜率较大
//        if (emitter != nil) {
//            [emitter release];
//        }
        
//        emitter = [CCParticleRain node];
//        emitter.gravity = ccp(90, -680);
//        emitter.skewX = -10;
//        emitter.scaleX = 1;
//        emitter.scaleY = 8;
//        emitter.emissionRate = 100;
//        emitter.position = worldPos(0.5, 1.0);//ccp(190, 480);
//        emitter.angle = 320;
  
        emitter = [self rainParticle];
        [effectStage.maskLayer addChild:emitter];

        [super skillEffect];
        
        [self performSelectorInBackground:@selector(skillDuration) withObject:nil];
    }
}


- (void)skillDuration {
    NSMutableArray* deadMonsters = [[NSMutableArray alloc] initWithCapacity:10];
    for (int i = 0; i < 6; ++i) {
        for (ASJMonster* monster in effectStage.monsters) {
            monster.currentMovingSpeed = ccp(monster.movingSpeed.x/3.0, monster.movingSpeed.y/3.0);
            monster.currentHP -= self.skillPowser;
            
            if (monster.currentHP > 0) {
                // blink
                CCTintTo* blink = [CCTintTo actionWithDuration:0.2 red:255.0 green:0.0 blue:0.0];
                [monster.currentSkill.skillSprite runAction:blink];
                //effectStage.firstMonster
            } else {
                // fade out
                CCFadeOut* fadeout = [CCFadeOut actionWithDuration:0.3];
                
                //[effectStage.firstMonster.currentSkill.skillSprite stopAllActions];
                [monster.currentSkill.skillSprite runAction:fadeout];
                [deadMonsters addObject:monster];
                if ([effectStage.monsters count])
                {
                    monster = [effectStage.monsters objectAtIndex:0];
                }
                else
                {
                    effectStage.firstMonster = nil;
                    killMonsterCont++;
                    if (killMonsterCont == 5)
                    {
                        
                    }
                }
            }
        }

        [effectStage.monsters removeObjectsInArray:deadMonsters];
        [deadMonsters removeAllObjects];
        [NSThread sleepForTimeInterval:1.0];
    }
    [deadMonsters release];
    
    for (ASJMonster* monster in effectStage.monsters) {
        monster.currentMovingSpeed = monster.movingSpeed;
    }
    
    [effectStage.maskLayer removeChild:emitter cleanup:YES];
}

@end
